#include "mainwidget.h"
#include "ui_mainwidget.h"
#include "QFileDialog"


MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{

    ui->setupUi(this);
    pLoginWidget = NULL;
    pSinaWidget = NULL;
    pRenrenWidget = NULL;
    pRssReader = NULL;
    currentSelected = 0;

    QPalette palette;
    QPixmap pixmap(":/images/renren/theme1.png");


    if (pixmap.isNull())
        qDebug("NULL!!!");

    palette.setBrush(this->backgroundRole(), QBrush(pixmap));

    this->setPalette(palette);

    loadClient(Login);

    connect(this,SIGNAL(clicked(Position,int)),this,SLOT(response(Position,int)),Qt::UniqueConnection);



}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::loadClient(Client client){


    QPixmap pixmap(":/images/more.png");
    ui->label_up_15->setPixmap(pixmap);

    if (client == Login && currentClient != Login){

        if (pLoginWidget == NULL)
        {
            pLoginWidget = new LoginWidget(this);
            connect(pLoginWidget,SIGNAL(sendToMainWidget(Client)),this,SLOT(loginSuccessed(Client)),Qt::UniqueConnection);
        }
        removeCurrentClient();
        ui->photoLabel->setPixmap(QPixmap(""));
        ui->layout->addWidget(pLoginWidget);
        pLoginWidget->show();
        currentClient = Login;
        this->currentSelected = 15;
    }

    else if (client == Sina && currentClient != Sina){
        removeCurrentClient();
        changeSelecedPixmap(15,0);

        QPixmap pixmap(":/images/sina/weibologo.png");

        if (pixmap.isNull())
            qDebug("NULL!!!");

        if ( 0 == ui->label_up_1->pixmap())
        {
            if (!clientList.contains(Sina))
            {
                 clientList.push_back(Sina);
            ui->label_up_1->setPixmap(pixmap);
            this->currentSelected = 1;
            }
        }
        else if ( 0 == ui->label_up_2->pixmap())
        {
            if (!clientList.contains(Sina))
            {
                 clientList.push_back(Sina);
            ui->label_up_2->setPixmap(pixmap);
            this->currentSelected = 2;
            }
        }
        else
        {
            if (!clientList.contains(Sina))
            {
                 clientList.push_back(Sina);
                ui->label_up_3->setPixmap(pixmap);
                this->currentSelected = 3;
            }
        }

        Account *pAccount = pLoginWidget->getSinaAccount();


        if (pSinaWidget == NULL)
        {
            SinaWeibo *pSinaWeibo = new SinaWeibo(pAccount);
            pSinaWeibo->initHomePageStatus();
            pSinaWeibo->initEmotions();
            delete pSinaWeibo;
        }

        if (pSinaWidget == NULL)
            pSinaWidget = new SinaWidget(pAccount,this);

        QPixmap headPhoto = pSinaWidget->getPixmapFromUrl(pAccount->getProfileImageUrl());


        if (sinaPhoto.isNull())
                sinaPhoto = headPhoto.scaled(widget_width,widget_height);

        ui->photoLabel->setPixmap(sinaPhoto);

        ui->layout->addWidget(pSinaWidget);
        pSinaWidget->show();

        currentClient = Sina;

        connect(this,SIGNAL(sendSignalToSina(int)),pSinaWidget,
                SLOT(receiveSignalFromMainWidget(int)));

        connect(pSinaWidget,SIGNAL(forwardToOtherClientSignal(QString)),
                this,SLOT(forwardToOtherClientSlot(QString)),Qt::UniqueConnection);




        if (pRenrenWidget != NULL){

            if (false == disconnect(this,SIGNAL(forward_to_otheClientSignal(QString)),
                               pSinaWidget,SLOT(forward_from_otherClient(QString))))
            {
                qDebug("Client = Sina : disconnect failed");
            }
            connect(this,SIGNAL(forward_to_otheClientSignal(QString)),
                    pRenrenWidget,SLOT(forward_from_otherClient(QString)),Qt::UniqueConnection);
        }


    }

    else if (client == Rss && currentClient != Rss)
    {

        if (pRssReader == NULL)
            pRssReader = new RssReader(this);
        removeCurrentClient();
        changeSelecedPixmap(15,0);

        QPixmap pixmap(":/images/rss/rsslogo.png");



        if (pixmap.isNull())
            qDebug("NULL!!!");

        if ( 0 == ui->label_up_1->pixmap())
        {
            if (!clientList.contains(Rss))
            {
                 clientList.push_back(Rss);
                 ui->label_up_1->setPixmap(pixmap);
                  this->currentSelected = 1;
            }
        }
        else if ( 0 == ui->label_up_2->pixmap())
        {
            if (!clientList.contains(Rss))
            {
                clientList.push_back(Rss);
                ui->label_up_2->setPixmap(pixmap);
                 this->currentSelected = 2;
            }
        }
        else
        {
            if (!clientList.contains(Rss))
            {
                clientList.push_back(Rss);
                ui->label_up_3->setPixmap(pixmap);
                 this->currentSelected = 3;
            }
        }

        ui->layout->addWidget(pRssReader);
        pRssReader->show();
        currentClient = Rss;
    }
    else if (client == RenRen && currentClient != RenRen)
    {
        changeSelecedPixmap(15,0);
        QPixmap pixmap(":images/renren/renrenlogo.png");

        if ( 0 == ui->label_up_1->pixmap())
        {
            if (!clientList.contains(RenRen))
            {
                clientList.push_back(RenRen);
            ui->label_up_1->setPixmap(pixmap);
             this->currentSelected = 1;
            }
        }
        else if ( 0 == ui->label_up_2->pixmap()) {
            if (!clientList.contains(RenRen))
            {
                clientList.push_back(RenRen);
            ui->label_up_2->setPixmap(pixmap);
             this->currentSelected = 2;
            }
        }
        else
        {
            if (!clientList.contains(RenRen))
            {
                clientList.push_back(RenRen);
            ui->label_up_3->setPixmap(pixmap);
             this->currentSelected = 3;
            }
        }

        removeCurrentClient();

        if (pRenrenWidget == NULL)
        {
            pRenrenWidget = new RenrenWidget();


            pRenrenWidget->prenrenAccount = new RenRenAccount;
            pRenrenWidget->prenrenAccount = pLoginWidget->getRenRenAccount();
            pRenrenWidget->pFeedApi->account = *pLoginWidget->getRenRenAccount();
            pRenrenWidget->pGetFriendsApi->account = *pLoginWidget->getRenRenAccount();
            pRenrenWidget->pStatusApi->account = *pLoginWidget->getRenRenAccount();
            pRenrenWidget->pUserApi->account = *pLoginWidget->getRenRenAccount();
            pRenrenWidget->pBlogApi->account = *pLoginWidget->getRenRenAccount();
            pRenrenWidget->pPhotoApi->account = *pLoginWidget->getRenRenAccount();
            pRenrenWidget->pShareApi->account = *pLoginWidget->getRenRenAccount();
            pRenrenWidget->getFeed();
            pRenrenWidget->getFriends();
        }
        QPixmap headPhoto = pRenrenWidget->getPixmap();


        if (renrenPhoto.isNull())
                renrenPhoto = headPhoto.scaled(widget_width,widget_height);

        ui->photoLabel->setPixmap(renrenPhoto);
        ui->layout->addWidget(pRenrenWidget);
        pRenrenWidget->show();

        currentClient = RenRen;

        if (pSinaWidget != NULL)
        {

            if ( false == disconnect(this,SIGNAL(forward_to_otheClientSignal(QString))
                       ,pRenrenWidget,SLOT(forward_from_otherClient(QString))) )
            {
                qDebug("Client = RenRen : disconnect failed");
            }
            connect(this,SIGNAL(forward_to_otheClientSignal(QString)),
                    pSinaWidget,SLOT(forward_from_otherClient(QString)),Qt::UniqueConnection);
        }

        connect(pRenrenWidget,SIGNAL(forwardToOtherClientSignal(QString)),
                this,SLOT(forwardToOtherClientSlot(QString)),Qt::UniqueConnection);

        connect(this,SIGNAL(sendSignalToRenren(int)),pRenrenWidget,SLOT(receiveSignalFromMainWidget(int)),Qt::UniqueConnection);
    }
    else
        return ;
}

void MainWidget::mousePressEvent(QMouseEvent *pEvent){

    if (currentClient == Login && clientList.size() == 0){
        pEvent->ignore();
        return;
    }

    int x = pEvent->x();
    int y = pEvent->y();

    int reminderOfX = x % widget_width;
    int reminderOfY = y % widget_height;

    if ( (reminderOfX > distance && reminderOfX < widget_width - distance)
            &&(reminderOfY > distance && reminderOfY < widget_height - distance)
            &&( x / widget_width < 1 || y / widget_height < 1)
            &&  y / widget_height < 9
            &&  x / widget_width  < 16
            )
    {
        if ( x / widget_width < 1 && currentClient != None){
//            qDebug("你点击了左边的第"+toQByteArray(y / widget_height)+"个Label");
            emit clicked(Left,y / widget_height);
        }
        else if ( y / widget_height < 1){
//            qDebug("你点击了上边的第"+toQByteArray(x / widget_width)+"个Label");
                emit clicked(Up,x / widget_width);
        }
    }
}

void MainWidget::response(Position pos, int index){


    if ( pos == Left ){
        if ( index >0 && index < 9){
            if (currentClient == Sina)
                emit sendSignalToSina(index);
            else if (currentClient == RenRen)
                emit sendSignalToRenren(index);
        }
        else {
            qDebug("pos = left ,index value Error!!!");
            return ;
        }
    }
    else if ( pos == Up){
        switch ( index ){
        case 1:
            if (ui->up_1->isVisible()){
                if (index <= clientList.size())
                {
                    Client client = clientList.at(index-1);
                    changeSelecedPixmap(this->currentSelected,0);
                    loadClient(client);
                    changeSelecedPixmap(index,1);
                    this->currentSelected = index;
                }
            }
            break;
        case 2:
            if (ui->up_2->isVisible()){
                if (index <= clientList.size())
                {
                    Client client = clientList.at(index-1);
                    changeSelecedPixmap(this->currentSelected,0);
                    loadClient(client);
                    changeSelecedPixmap(index,1);
                    this->currentSelected = index;
                }
            }
            break;
        case 3:
            if (ui->up_3->isVisible()){
                if (index <= clientList.size())
                {
                    Client client = clientList.at(index-1);
                    changeSelecedPixmap(this->currentSelected,0);
                    loadClient(client);
                    changeSelecedPixmap(index,1);
                    this->currentSelected = index;
                }
            }
            break;
        case 4:
            if (ui->up_4->isVisible()){

            }
            break;
        case 5:
            if (ui->up_5->isVisible()){

            }
            break;
        case 6:
            if (ui->up_6->isVisible()){

            }
            break;
        case 7:
            if (ui->up_7->isVisible()){

            }
            break;
        case 8:
            if (ui->up_8->isVisible()){

            }
            break;
        case 9:
            if (ui->up_9->isVisible()){

            }
            break;
        case 10:
            if (ui->up_10->isVisible()){

            }
            break;
        case 11:
            if (ui->up_11->isVisible()){

            }
            break;
        case 12:
            if (ui->up_12->isVisible()){

            }
            break;
        case 13:
            if (ui->up_13->isVisible()){

            }
            break;
        case 14:
            if (ui->up_14->isVisible()){

            }
            break;
        case 15:
            if (ui->up_15->isVisible()){
                Client client = Login;
                changeSelecedPixmap(this->currentSelected,0);
                loadClient(client);
                changeSelecedPixmap(index,1);
                this->currentSelected = index;
            }
            break;
        default:
            break;
        }
    }
    else{
        qDebug("In response(Position pos, int index):Position Error!!!");
        return ;
    }
}

QByteArray MainWidget::toQByteArray(int num){
    return QString::number(num).toLocal8Bit();
}

void MainWidget::removeCurrentClient(){

    switch (currentClient){

    case None:
        break;
    case Login:
        if (!ui->layout->isEmpty()){
            ui->layout->removeWidget(pLoginWidget);
            pLoginWidget->hide();

        }
        break;
    case Sina:
        if (!ui->layout->isEmpty()){
            ui->layout->removeWidget(pSinaWidget);
            pSinaWidget->hide();

            bool fResult = true;
            fResult = fResult && QObject::disconnect(this,SIGNAL(sendSignalToSina(int)));
            fResult = fResult && QObject::disconnect(this,SIGNAL(forward_to_otheClientSignal(QString)));
            fResult = fResult && QObject::disconnect(pSinaWidget,SIGNAL(forwardToOtherClientSignal(QString)));
            if (fResult == false)
            {
                 qDebug("case Sina : disconnect failed");
            }

        }
    case RenRen:
        if (!ui->layout->isEmpty()){
            ui->layout->removeWidget(pRenrenWidget);
            pRenrenWidget->hide();

            bool fResult = true;

            fResult = fResult && QObject::disconnect(this,SIGNAL(forward_to_otheClientSignal(QString)));
            fResult = fResult && QObject::disconnect(this,SIGNAL(sendSignalToRenren(int)));
            fResult = fResult && QObject::disconnect(pRenrenWidget,SIGNAL(forwardToOtherClientSignal(QString)));

            if (fResult == false)
            {
                 qDebug("case RenRen : disconnect failed");
            }


        }
    case Rss:
        if (!ui->layout->isEmpty()){
            ui->layout->removeWidget(pRssReader);
            pRssReader->hide();
        }

    default:
        break;
    }
}

void MainWidget::loginSuccessed(Client client){

    if (client == Sina)
    {
        loadClient(Sina);
        changeSelecedPixmap(this->currentSelected,1);
        return ;
    }
    if (client == Rss)
    {
        loadClient(Rss);
        changeSelecedPixmap(this->currentSelected,1);
        return ;
    }
    if(client == RenRen)
    {
        loadClient(RenRen);
        changeSelecedPixmap(this->currentSelected,1);
        return ;
    }
}

void MainWidget::forwardToOtherClientSlot(QString str)
{
    qDebug("in MainWidget:"+str.toLocal8Bit());

    if (pSinaWidget == NULL || pRenrenWidget == NULL)
    {
        QMessageBox::information(NULL,tr("转发错误"),tr("不存在其它客户端"),QMessageBox::Yes);
    }
    else
    {
        emit forward_to_otheClientSignal(str);
    }
}

void MainWidget::changeSelecedPixmap(int index, int type)
{


    QString styleSheet="QWidget#%1%2{background-image:url(:/images/upselected.png)}";
    if (type == 0)//remove
    {
        QString tmp = "";
        switch (index)
        {
        case 1:
            ui->up_1->setStyleSheet(tmp);
            break;
        case 2:
            ui->up_2->setStyleSheet(tmp);
            break;
        case 3:
            ui->up_3->setStyleSheet(tmp);
            break;
        case 15:
            ui->up_15->setStyleSheet(tmp);
            break;
        }
    }
    else if (type == 1)//add
    {
        styleSheet = styleSheet.arg("up_").arg(QString::number(index));
        switch (index)
        {
        case 1:
            ui->up_1->setStyleSheet(styleSheet);
            break;
        case 2:
            ui->up_2->setStyleSheet(styleSheet);
            break;
        case 3:
            ui->up_3->setStyleSheet(styleSheet);
            break;
        case 15:
            ui->up_15->setStyleSheet(styleSheet);
            break;
    }
    }
    else{
        qDebug("changeSelectedPixmap type error!!!");
    }
}

void MainWidget::closeEvent(QCloseEvent *e)
{
    if (pSinaWidget != NULL)
        pSinaWidget->close();

    qDebug("MainWidget::closeEvent");

    exit(0);
}
